﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HMS.DL;


namespace HMS.PL.Forms
{
    public partial class CreateNodeType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hid_HierarchyId.Value = Request.QueryString["hier_id"].ToString();
                hid_VersionId.Value = Request.QueryString["vers_id"].ToString();
                hid_NodeId.Value = Request.QueryString["id"].ToString();
                hid_NodeTypeId.Value = Request.QueryString["type_id"].ToString();
                hid_TableName.Value = Request.QueryString["table_name"].ToString();
                //hid_txtdate.Value = Request.QueryString["txtdate"].ToString();
            }
            else
            {


                ConfigHierData objConfigData = new ConfigHierData();
                string fn = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);

                objConfigData.HierarchyId = Convert.ToInt32(hid_HierarchyId.Value);
                objConfigData.VersionId = Convert.ToInt32(hid_VersionId.Value);
                objConfigData.NodeTypeId = Convert.ToInt32(hid_NodeTypeId.Value);
                objConfigData.ParentId = Convert.ToInt32(hid_NodeId.Value);
                objConfigData.NewNodeTypeName = txt_NodeTypeName.Value;
                objConfigData.isOwnParent = Convert.ToBoolean(chkOwnParent.Checked);
                objConfigData.imageName = fn;

                String scriptText = "";

                //validate for duplicate node name within 
                // same hirarchy and version
                Boolean isDuplicate = objConfigData.ValidateNodeTypeName();
                if (isDuplicate == true)
                {
                    scriptText = "<script language='javascript'>";
                    //original code
                    // scriptText += "document.body.onload=function(){ DisableTab('" + js.Serialize(Session["IsTabAllowed"]) + "')};";
                    scriptText += "CloseDup();";
                    scriptText += "</script>";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "Pop", scriptText);
                }
                else
                {

                    //size validation
                    //fileupload.PostedFile.ContentLength
                    if ((fileupload.PostedFile != null) && (fileupload.PostedFile.ContentLength > 0))
                    {

                        string SaveLocation = Server.MapPath("..\\NodeImages") + "\\" + fn;
                        try
                        {
                            fileupload.PostedFile.SaveAs(SaveLocation);
                            //Response.Write("The file has been uploaded." + fileupload1.PostedFile.FileName);
                            CreateNodeTypes(objConfigData,fn);
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Error: " + ex.Message);
                            //Note: Exception.Message returns a detailed message that describes the current exception. 
                            //For security reasons, we do not recommend that you return Exception.Message to end users in 
                            //production environments. It would be better to put a generic error message. 
                        }
                    }
                    else
                    {
                        CreateNodeTypes(objConfigData,"");
                    }
                }
            }
        }

        private void CreateNodeTypes(ConfigHierData objConfigData,string fname)
        {
            try
            {


                String scriptText = "";
                //ConfigHierData objConfigData = new ConfigHierData();
                //objConfigData.NewNodeTypeId = Request.QueryString["newnode_typeid"].ToString();
                Int32 ident = objConfigData.CreateNodeType();

                scriptText = "<script language='javascript'>";
                //original code
                // scriptText += "document.body.onload=function(){ DisableTab('" + js.Serialize(Session["IsTabAllowed"]) + "')};";
                scriptText += "Close('" + ident.ToString() + "','" + fname + "','" + txt_NodeTypeName.Value + "','" + chkOwnParent.Checked.ToString().ToLower() + "');";

                scriptText += "</script>";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "Pop", scriptText);

            }
            catch (Exception ex)
            {
                // Util.WritErrorLog(ClsName, "CreateNodeType", "CreateNodeType", "", ex);
                throw ex;
            }
        }
    }
}